package auxiliares {
	import flash.display.Graphics;
	import flash.geom.Point;

	public class SemiCircle {
	
	
		public function drawSemiCircle(g:Graphics, centerX:Number, centerY:Number, startAngle:Number, endAngle:Number, radius:Number, direction:Number ):void
		{
			g.clear();
			endAngle *= (Math.PI / 180);
			startAngle *= (Math.PI / 180);
			if (endAngle != startAngle)
			{
				g.lineStyle(-1, 0x99CCFF, 0.0);
				
				//g.lineTo(0, -radius);
				g.beginFill(0x99CCFF, 0.3);
				drawArc(g, centerX, centerY, startAngle, endAngle, radius, direction);
				g.lineTo(centerX,centerY);
				g.endFill();
			}
		}
		
		public function drawArc(g:Graphics, centerX:Number, centerY:Number, startAngle:Number, endAngle:Number, radius:Number, direction:Number): void
		{
			var difference:Number = Math.abs(endAngle - startAngle);
			
			var divisions:Number = Math.floor(difference / (Math.PI / 4))+1;
			
			var span:Number    = direction * difference / (2 * divisions);
			var controlRadius:Number    = radius / Math.cos(span);
			
			g.moveTo(centerX + (Math.cos(startAngle)*radius), centerY + Math.sin(startAngle)*radius);
			var controlPoint:Point;
			var anchorPoint:Point;
			for(var i:Number=0; i<divisions; ++i)
			{
				endAngle    = startAngle + span;
				startAngle  = endAngle + span;
				
				controlPoint = new Point(centerX+Math.cos(endAngle)*controlRadius, centerY+Math.sin(endAngle)*controlRadius);
				anchorPoint = new Point(centerX+Math.cos(startAngle)*radius, centerY+Math.sin(startAngle)*radius);
				g.curveTo(
					controlPoint.x,
					controlPoint.y,
					anchorPoint.x,
					anchorPoint.y
				);
			}
		}
	}
}